import { _decorator, Component, EventKeyboard, EventMouse, Input, input, Node, tween, UITransform, Vec3 } from 'cc';
import { StateEngine } from './StateEngine';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {
    @property(Node)
    public player: Node;
    engine: StateEngine;
    playerEntity: any;
    keyState: boolean;

    // 改用 Set 记录按下的按键
    private pressedKeys = new Set<number>();
    start() {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }
    onMouseMove(event: EventMouse) {
        // 计算鼠标坐标到玩家坐标的角度
        // 1. 获取鼠标在游戏世界中的坐标
        const mousePos = event.getUILocation();
        const worldPos = this.node.parent.getComponent(UITransform)!.convertToNodeSpaceAR(
            new Vec3(mousePos.x, mousePos.y, 0)
        );

        // 2. 计算玩家到鼠标的向量
        const playerPos = this.node.position;
        const direction = new Vec3(worldPos.x - playerPos.x, worldPos.y - playerPos.y);

        // 3. 计算角度并转换到0-360范围
        let angle = Math.atan2(direction.y, direction.x) * 180 / Math.PI;
        angle = (angle + 360) % 360; // 标准化角度值

        // 4. 发送旋转指令（添加90度修正坐标系差异）
        this.engine.sendCommand('rotate', angle - 90);

        // 5. 立即更新本地预览（可选）
        // this.updateRotation(angle);
    }
    // 监听键盘方向键，向外发送指令
    onKeyDown(event: EventKeyboard) {
        this.pressedKeys.add(event.keyCode);

        this.sendCommand()
        this.schedule(this.sendCommand, 0.1, Infinity, 0)
    }
    onKeyUp(event: EventKeyboard) {
        this.pressedKeys.delete(event.keyCode);
        if (this.pressedKeys.size === 0) {
            this.unschedule(this.sendCommand); // 没有按键时停止检测
        }
    }

    sendCommand() {
        const engine = this.engine

        this.pressedKeys.forEach(keyCode => {
            switch (keyCode) {
                case 87: // W
                    engine.sendCommand('move', 'up');
                    break;
                case 83: // S
                    engine.sendCommand('move', 'down');
                    break;
                case 65: // A
                    engine.sendCommand('move', 'left');
                    break;
                case 68: // D
                    engine.sendCommand('move', 'right');
                    break;
                case 32: // Space
                    engine.sendCommand('shoot', null);
                    break;
                // case 17: // Ctrl
                //     engine.sendCommand('rotate', 5);
                //     break;
            }
        });
    }

    protected onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    updatePosition(x: number, y: number) {
        this.node.position = new Vec3(x, y, 0);
    }

    updateRotation(angle: number) {
        this.node.angle = angle;
    }

    update(deltaTime: number) {

    }
}

